package com.ly.ocw.web.admin;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.ocw.entity.CrmMember;
import com.ly.ocw.exception.ServerException;
import com.ly.ocw.service.CrmMemberService;
import com.ly.ocw.service.weixin.WeixinAccessTokenService;
import com.ly.ocw.service.weixin.WeixinCardService;
import com.ly.ocw.service.weixin.WeixinUserCardService;

/**
 * 会员卡 链接回调 Controller
 * 
 * @author Peter
 *
 */
@Controller
@RequestMapping(value = "/cardcallback")
public class CardCallbackController {

	private static Logger logger = LoggerFactory.getLogger(CardCallbackController.class);

	@Autowired
	private WeixinCardService weixinCardService;

	@Autowired
	private WeixinUserCardService weixinUserCardService;

	@Autowired
	private WeixinAccessTokenService weixinAccessTokenService;

	@Autowired
	private CrmMemberService crmMemberService;

	/**
	 * 手工激活会员卡
	 * 
	 * @param model
	 * @return
	 * @throws ServerException
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	@RequestMapping(value = "activate", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String activateCard(HttpServletRequest request, HttpServletResponse response, Model model) throws ClientProtocolException, IOException, ServerException {
		String cardId = request.getParameter("card_id");
		String openid = request.getParameter("openid");
		String encryptCode = request.getParameter("encrypt_code");
		logger.info("card_id:" + cardId);
		logger.info("openid:" + openid);
		logger.info("encryptCode:" + encryptCode);
		logger.info("RequestInfo:{RemoteAddr:" + request.getRemoteAddr() + ",LocalName:" + request.getLocalName() + ",LocalPort:" + request.getLocalPort() + ",Method"
				+ request.getMethod() + ",PathInfo:" + request.getPathInfo() + ",QueryString:" + request.getQueryString() + ",RemoteAddr:" + request.getRemoteAddr()
				+ ",RemoteHost:" + request.getRemoteHost() + ",RemotePort:" + request.getRemotePort() + ",RequestURI:" + request.getRequestURI() + "}");

		Enumeration<String> pnameEnu = request.getParameterNames();
		logger.info("----- 参数Parameters Start ----");
		while (pnameEnu.hasMoreElements()) {
			logger.info("{" + pnameEnu.nextElement() + ":" + request.getParameter(pnameEnu.nextElement()) + "}");
		}
		logger.info("----- 参数Parameters End ----");

		// String code = weixinCardService.decodeEncryptCode(encryptCode);

		String respStr;
		try {
			String code = weixinCardService.DecryptCode(encryptCode);
			respStr = weixinCardService.activateCard(cardId, code, openid);
		} catch (Exception e) {
			
			logger.error("激活会员卡失败！",e);
			respStr="激活会员卡失败";
		}
		respStr="激活会员卡成功";
		return respStr;
	}

	// custom_url 我的积分
	@RequestMapping(value = "custom_url", method = RequestMethod.GET)
	public String customUrl(HttpServletRequest request, HttpServletResponse response, Model model) {
		logger.info("custom_url 我的积分");
		String cardId = request.getParameter("card_id");
		String openid = request.getParameter("openid");
		String encryptCode = request.getParameter("encrypt_code");
		logger.info("card_id:" + cardId);
		logger.info("openid:" + openid);
		logger.info("encryptCode:" + encryptCode);
		
		try {
			//Code解码
			String code = weixinCardService.DecryptCode(encryptCode);
			
			CrmMember crmMember= crmMemberService.getByCardIdAndCode(cardId, code);
			
			model.addAttribute("crmMember", crmMember);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			model.addAttribute("error", e.getMessage());
		}
		
		return "cardcallback/custom_url";
	}

	// promotion_url 孩子王APP
	@RequestMapping(value = "promotion_url", method = RequestMethod.GET)
	public String promotionUrl(Model model) {

		return "cardcallback/promotion_url";
	}

	// custom_field1_url 等级
	@RequestMapping(value = "custom_field1_url", method = RequestMethod.GET)
	public String customField1Url(HttpServletRequest request, HttpServletResponse response, Model model) {
		logger.info("custom_url 等级");
		String cardId = request.getParameter("card_id");
		String openid = request.getParameter("openid");
		String encryptCode = request.getParameter("encrypt_code");
		logger.info("card_id:" + cardId);
		logger.info("openid:" + openid);
		logger.info("encryptCode:" + encryptCode);
		
		//CrmMember crmMember= crmMemberService.getByCardIdAndOpenid(cardId, openid);
		
		//测试数据
		CrmMember crmMember= new CrmMember();
		crmMember.setTierLevel("ZC");
		crmMember.setPromoteExperiencePointsNeed("3000.00");
		crmMember.setPointsBalance("5000");
		crmMember.setRelegationExperiencePoints("100.00");
		
		
		model.addAttribute("crmMember", crmMember);
		return "cardcallback/custom_field1_url";
	}

	// custom_field2_url 优惠券
	@RequestMapping(value = "custom_field2_url", method = RequestMethod.GET)
	public String customField2Url(Model model) {

		return "cardcallback/custom_field2_url";
	}

	// custom_cell1 微购商城
	@RequestMapping(value = "custom_cell1", method = RequestMethod.GET)
	public String customCell1(Model model) {

		return "cardcallback/custom_cell1";
	}

}
